{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# CrypTen on AWS Instances\n",
    "\n",
    "Our previous tutorials have covered the essentials of using CrypTen on our local machines. We also provides a script `aws_launcher.py` in the `scripts` directory that will allow you to compute on encrypted data on multiple AWS instances. \n",
    "\n",
    "For example, if Alice has a classifier on one AWS instance and Bob has data on another AWS instance, `aws_launcher.py` will allow Alice and Bob to classify the data without revealing their respective private information (just as we did in Tutorial 4). \n",
    "\n",
    "## Using the Launcher Script\n",
    "\n",
    "The steps to follow are:\n",
    "<ol>\n",
    "    <li> First, create multiple AWS instances with public AMI \"Deep Learning AMI (Ubuntu) Version 24.0\", and record the instance IDs. </li>\n",
    "    <li> Install PyTorch, CrypTen and dependencies of the program to be run on all AWS instances.</li>\n",
    "    <li> Run `aws_launcher.py` on your local machine, as we explain below.</li>\n",
    "</ol>\n",
    "The results are left on the AWS instances. Log messages will be printed on your local machine by launcher script.\n",
    "\n",
    "## Sample Run\n",
    "\n",
    "The following cell shows a sample usage of the `aws_launcher.py` script. Note, however, that the command in the cell will not work as is: please replace the parameters used with appropriate ones with your own AWS instances, usernames, and `.ssh` keys (see documentation in the `aws_launcher.py` script). "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 0,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Running world size 2 with instances: [ec2.Instance(id='i-0615648acb65e87ed'), ec2.Instance(id='i-0c9ae083275ab0b73')]\n",
      "Connecting to i-0615648acb65e87ed...\n",
      "Connected to i-0615648acb65e87ed\n",
      "Connecting to i-0c9ae083275ab0b73...\n",
      "Connected to i-0c9ae083275ab0b73\n",
      "Uploading `/private/home/shobha/CrypTen/examples/mpc_linear_svm/launcher.py` to i-0615648acb65e87ed...\n",
      "Uploading `/private/home/shobha/CrypTen/examples/mpc_linear_svm/mpc_linear_svm.py` to i-0615648acb65e87ed...\n",
      "Uploading `/private/home/shobha/CrypTen/examples/mpc_linear_svm/launcher.py` to i-0c9ae083275ab0b73...\n",
      "Uploading `/private/home/shobha/CrypTen/examples/mpc_linear_svm/mpc_linear_svm.py` to i-0c9ae083275ab0b73...\n",
      "`/private/home/shobha/CrypTen/examples/mpc_linear_svm/launcher.py` uploaded to i-0615648acb65e87ed.\n",
      "`/private/home/shobha/CrypTen/examples/mpc_linear_svm/mpc_linear_svm.py` uploaded to i-0615648acb65e87ed.\n",
      "`/private/home/shobha/CrypTen/examples/mpc_linear_svm/launcher.py` uploaded to i-0c9ae083275ab0b73.\n",
      "`/private/home/shobha/CrypTen/examples/mpc_linear_svm/mpc_linear_svm.py` uploaded to i-0c9ae083275ab0b73.\n",
      "[i-0615648acb65e87ed] total 16\r\n",
      "[i-0615648acb65e87ed] drwxrwxr-x  2 ec2-user ec2-user 4096 Sep  4 20:41 .\r\n",
      "[i-0615648acb65e87ed] drwx------ 22 ec2-user ec2-user 4096 Sep  4 20:41 ..\r\n",
      "[i-0615648acb65e87ed] -rwxrwxr-x  1 ec2-user ec2-user 2698 Sep  4 20:41 launcher.py\r\n",
      "[i-0615648acb65e87ed] -rw-rw-r--  1 ec2-user ec2-user 3397 Sep  4 20:41 mpc_linear_svm.py\r\n",
      "[i-0615648acb65e87ed] \n",
      "[i-0c9ae083275ab0b73] total 16\r\n",
      "[i-0c9ae083275ab0b73] drwxrwxr-x  2 ec2-user ec2-user 4096 Sep  4 20:41 .\r\n",
      "[i-0c9ae083275ab0b73] drwx------ 22 ec2-user ec2-user 4096 Sep  4 20:41 ..\r\n",
      "[i-0c9ae083275ab0b73] -rwxrwxr-x  1 ec2-user ec2-user 2698 Sep  4 20:41 launcher.py\r\n",
      "[i-0c9ae083275ab0b73] -rw-rw-r--  1 ec2-user ec2-user 3397 Sep  4 20:41 mpc_linear_svm.py\r\n",
      "[i-0c9ae083275ab0b73] \n",
      "Run command: export WORLD_SIZE=2; export RENDEZVOUS=env://; export MASTER_ADDR=172.31.67.210; export MASTER_PORT=29500; export RANK=0; cd aws-launcher-tmp-57f5182c-cf54-11e9-8e9f-90e2badb2844 ;  ./launcher.py --features 50 --examples 100 --epochs 50 --lr 0.5 --skip_plaintext\n",
      "Run command: export WORLD_SIZE=2; export RENDEZVOUS=env://; export MASTER_ADDR=172.31.67.210; export MASTER_PORT=29500; export RANK=1; cd aws-launcher-tmp-57f5182c-cf54-11e9-8e9f-90e2badb2844 ;  ./launcher.py --features 50 --examples 100 --epochs 50 --lr 0.5 --skip_plaintext\n",
      "[i-0c9ae083275ab0b73] INFO:root:==================\r\n",
      "[i-0c9ae083275ab0b73] INFO:root:DistributedCommunicator with rank 1\r\n",
      "[i-0c9ae083275ab0b73] INFO:root:==================\r\n",
      "[i-0c9ae083275ab0b73] INFO:root:World size = 2\r\n",
      "[i-0c9ae083275ab0b73] \n",
      "[i-0615648acb65e87ed] INFO:root:==================\r\n",
      "[i-0615648acb65e87ed] INFO:root:DistributedCommunicator with rank 0\r\n",
      "[i-0615648acb65e87ed] INFO:root:==================\r\n",
      "[i-0615648acb65e87ed] INFO:root:World size = 2\r\n",
      "[i-0615648acb65e87ed] INFO:root:==================\r\n",
      "[i-0615648acb65e87ed] INFO:root:CrypTen Training\r\n",
      "[i-0615648acb65e87ed] INFO:root:==================\r\n",
      "[i-0615648acb65e87ed] INFO:root:Epoch 1\r\n",
      "[i-0615648acb65e87ed] INFO:root:--- Accuracy 46.00%\r\n",
      "[i-0615648acb65e87ed] INFO:root:    Time 0.028704 (0.028704)\r\n",
      "[i-0615648acb65e87ed] INFO:root:Epoch 2\r\n",
      "[i-0615648acb65e87ed] INFO:root:--- Accuracy 49.00%\r\n",
      "[i-0615648acb65e87ed] INFO:root:    Time 0.015724 (0.022214)\r\n",
      "[i-0615648acb65e87ed] INFO:root:Epoch 3\r\n",
      "[i-0615648acb65e87ed] INFO:root:--- Accuracy 49.00%\r\n",
      "[i-0615648acb65e87ed] INFO:root:    Time 0.015827 (0.020085)\r\n",
      "[i-0615648acb65e87ed] INFO:root:Epoch 4\r\n",
      "[i-0615648acb65e87ed] INFO:root:--- Accuracy 50.00%\r\n",
      "[i-0615648acb65e87ed] INFO:root:    Time 0.015229 (0.018871)\r\n",
      "[i-0615648acb65e87ed] INFO:root:Epoch 5\r\n",
      "[i-0615648acb65e87ed] INFO:root:--- Accuracy 53.00%\r\n",
      "[i-0615648acb65e87ed] INFO:root:    Time 0.014997 (0.018096)\r\n",
      "[i-0615648acb65e87ed] INFO:root:Epoch 6\r\n",
      "[i-0615648acb65e87ed] INFO:root:--- Accuracy 54.00%\r\n",
      "[i-0615648acb65e87ed] INFO:root:    Time 0.015650 (0.017688)\r\n",
      "[i-0615648acb65e87ed] INFO:root:Epoch 7\r\n",
      "[i-0615648acb65e87ed] INFO:root:--- Accuracy 54.00%\r\n",
      "[i-0615648acb65e87ed] INFO:root:    Time 0.015314 (0.017349)\r\n",
      "[i-0615648acb65e87ed] INFO:root:Epoch 8\r\n",
      "[i-0615648acb65e87ed] INFO:root:--- Accuracy 55.00%\r\n",
      "[i-0615648acb65e87ed] INFO:root:    Time 0.015085 (0.017066)\r\n",
      "[i-0615648acb65e87ed] INFO:root:Epoch 9\r\n",
      "[i-0615648acb65e87ed] INFO:root:--- Accuracy 58.00%\r\n",
      "[i-0615648acb65e87ed] INFO:root:    Time 0.015701 (0.016915)\r\n",
      "[i-0615648acb65e87ed] INFO:root:Epoch 10\r\n",
      "[i-0615648acb65e87ed] INFO:root:--- Accuracy 59.00%\r\n",
      "[i-0615648acb65e87ed] INFO:root:    Time 0.015011 (0.016724)\r\n",
      "[i-0615648acb65e87ed] INFO:root:Epoch 11\r\n",
      "[i-0615648acb65e87ed] INFO:root:--- Accuracy 60.00%\r\n",
      "[i-0615648acb65e87ed] INFO:root:    Time 0.014846 (0.016553)\r\n",
      "[i-0615648acb65e87ed] INFO:root:Epoch 12\r\n",
      "[i-0615648acb65e87ed] INFO:root:--- Accuracy 63.00%\r\n",
      "[i-0615648acb65e87ed] INFO:root:    Time 0.015544 (0.016469)\r\n",
      "[i-0615648acb65e87ed] INFO:root:Epoch 13\r\n",
      "[i-0615648acb65e87ed] INFO:root:--- Accuracy 64.00%\r\n",
      "[i-0615648acb65e87ed] INFO:root:    Time 0.015662 (0.016407)\r\n",
      "[i-0615648acb65e87ed] INFO:root:Epoch 14\r\n",
      "[i-0615648acb65e87ed] INFO:root:--- Accuracy 66.00%\r\n",
      "[i-0615648acb65e87ed] INFO:root:    Time 0.015975 (0.016376)\r\n",
      "[i-0615648acb65e87ed] INFO:root:Epoch 15\r\n",
      "[i-0615648acb65e87ed] INFO:root:--- Accuracy 68.00%\r\n",
      "[i-0615648acb65e87ed] INFO:root:    Time 0.015527 (0.016320)\r\n",
      "[i-0615648acb65e87ed] INFO:root:Epoch 16\r\n",
      "[i-0615648acb65e87ed] INFO:root:--- Accuracy 70.00%\r\n",
      "[i-0615648acb65e87ed] INFO:root:    Time 0.015474 (0.016267)\r\n",
      "[i-0615648acb65e87ed] INFO:root:Epoch 17\r\n",
      "[i-0615648acb65e87ed] INFO:root:--- Accuracy 71.00%\r\n",
      "[i-0615648acb65e87ed] INFO:root:    Time 0.016068 (0.016255)\r\n",
      "[i-0615648acb65e87ed] INFO:root:Epoch 18\r\n",
      "[i-0615648acb65e87ed] INFO:root:--- Accuracy 73.00%\r\n",
      "[i-0615648acb65e87ed] INFO:root:    Time 0.015703 (0.016224)\r\n",
      "[i-0615648acb65e87ed] INFO:root:Epoch 19\r\n",
      "[i-0615648acb65e87ed] INFO:root:--- Accuracy 73.00%\r\n",
      "[i-0615648acb65e87ed] INFO:root:    Time 0.015182 (0.016170)\r\n",
      "[i-0615648acb65e87ed] INFO:root:Epoch 20\r\n",
      "[i-0615648acb65e87ed] INFO:root:--- Accuracy 77.00%\r\n",
      "[i-0615648acb65e87ed] INFO:root:    Time 0.015349 (0.016129)\r\n",
      "[i-0615648acb65e87ed] INFO:root:Epoch 21\r\n",
      "[i-0615648acb65e87ed] INFO:root:--- Accuracy 74.00%\r\n",
      "[i-0615648acb65e87ed] INFO:root:    Time 0.015506 (0.016099)\r\n",
      "[i-0615648acb65e87ed] INFO:root:Epoch 22\r\n",
      "[i-0615648acb65e87ed] INFO:root:--- Accuracy 76.00%\r\n",
      "[i-0615648acb65e87ed] INFO:root:    Time 0.015579 (0.016075)\r\n",
      "[i-0615648acb65e87ed] INFO:root:Epoch 23\r\n",
      "[i-0615648acb65e87ed] INFO:root:--- Accuracy 78.00%\r\n",
      "[i-0615648acb65e87ed] INFO:root:    Time 0.015471 (0.016049)\r\n",
      "[i-0615648acb65e87ed] INFO:root:Epoch 24\r\n",
      "[i-0615648acb65e87ed] INFO:root:--- Accuracy 79.00%\r\n",
      "[i-0615648acb65e87ed] INFO:root:    Time 0.015229 (0.016015)\r\n",
      "[i-0615648acb65e87ed] INFO:root:Epoch 25\r\n",
      "[i-0615648acb65e87ed] INFO:root:--- Accuracy 78.00%\r\n",
      "[i-0615648acb65e87ed] INFO:root:    Time 0.014884 (0.015970)\r\n",
      "[i-0615648acb65e87ed] INFO:root:Epoch 26\r\n",
      "[i-0615648acb65e87ed] INFO:root:--- Accuracy 80.00%\r\n",
      "[i-0615648acb65e87ed] INFO:root:    Time 0.015489 (0.015951)\r\n",
      "[i-0615648acb65e87ed] INFO:root:Epoch 27\r\n",
      "[i-0615648acb65e87ed] INFO:root:--- Accuracy 79.00%\r\n",
      "[i-0615648acb65e87ed] INFO:root:    Time 0.015419 (0.015931)\r\n",
      "[i-0615648acb65e87ed] INFO:root:Epoch 28\r\n",
      "[i-0615648acb65e87ed] INFO:root:--- Accuracy 83.00%\r\n",
      "[i-0615648acb65e87ed] INFO:root:    Time 0.015446 (0.015914)\r\n",
      "[i-0615648acb65e87ed] INFO:root:Epoch 29\r\n",
      "[i-0615648acb65e87ed] INFO:root:--- Accuracy 85.00%\r\n",
      "[i-0615648acb65e87ed] INFO:root:    Time 0.015117 (0.015887)\r\n",
      "[i-0615648acb65e87ed] INFO:root:Epoch 30\r\n",
      "[i-0615648acb65e87ed] INFO:root:--- Accuracy 85.00%\r\n",
      "[i-0615648acb65e87ed] INFO:root:    Time 0.015447 (0.015872)\r\n",
      "[i-0615648acb65e87ed] INFO:root:Epoch 31\r\n",
      "[i-0615648acb65e87ed] INFO:root:--- Accuracy 88.00%\r\n",
      "[i-0615648acb65e87ed] INFO:root:    Time 0.015865 (0.015872)\r\n",
      "[i-0615648acb65e87ed] INFO:root:Epoch 32\r\n",
      "[i-0615648acb65e87ed] INFO:root:--- Accuracy 89.00%\r\n",
      "[i-0615648acb65e87ed] INFO:root:    Time 0.015109 (0.015848)\r\n",
      "[i-0615648acb65e87ed] INFO:root:Epoch 33\r\n",
      "[i-0615648acb65e87ed] INFO:root:--- Accuracy 87.00%\r\n",
      "[i-0615648acb65e87ed] INFO:root:    Time 0.015537 (0.015838)\r\n",
      "[i-0615648acb65e87ed] INFO:root:Epoch 34\r\n",
      "[i-0615648acb65e87ed] INFO:root:--- Accuracy 93.00%\r\n",
      "[i-0615648acb65e87ed] INFO:root:    Time 0.015223 (0.015820)\r\n",
      "[i-0615648acb65e87ed] INFO:root:Epoch 35\r\n",
      "[i-0615648acb65e87ed] INFO:root:--- Accuracy 89.00%\r\n",
      "[i-0615648acb65e87ed] INFO:root:    Time 0.014812 (0.015792)\r\n",
      "[i-0615648acb65e87ed] INFO:root:Epoch 36\r\n",
      "[i-0615648acb65e87ed] INFO:root:--- Accuracy 95.00%\r\n",
      "[i-0615648acb65e87ed] INFO:root:    Time 0.015444 (0.015782)\r\n",
      "[i-0615648acb65e87ed] INFO:root:Epoch 37\r\n",
      "[i-0615648acb65e87ed] INFO:root:--- Accuracy 98.00%\r\n",
      "[i-0615648acb65e87ed] INFO:root:    Time 0.015514 (0.015775)\r\n",
      "[i-0615648acb65e87ed] INFO:root:Epoch 38\r\n",
      "[i-0615648acb65e87ed] INFO:root:--- Accuracy 94.00%\r\n",
      "[i-0615648acb65e87ed] INFO:root:    Time 0.015790 (0.015775)\r\n",
      "[i-0615648acb65e87ed] INFO:root:Epoch 39\r\n",
      "[i-0615648acb65e87ed] INFO:root:--- Accuracy 95.00%\r\n",
      "[i-0615648acb65e87ed] INFO:root:    Time 0.015150 (0.015759)\r\n",
      "[i-0615648acb65e87ed] INFO:root:Epoch 40\r\n",
      "[i-0615648acb65e87ed] INFO:root:--- Accuracy 97.00%\r\n",
      "[i-0615648acb65e87ed] INFO:root:    Time 0.015664 (0.015757)\r\n",
      "[i-0615648acb65e87ed] INFO:root:Epoch 41\r\n",
      "[i-0615648acb65e87ed] INFO:root:--- Accuracy 98.00%\r\n",
      "[i-0615648acb65e87ed] INFO:root:    Time 0.015070 (0.015740)\r\n",
      "[i-0615648acb65e87ed] INFO:root:Epoch 42\r\n",
      "[i-0615648acb65e87ed] INFO:root:--- Accuracy 98.00%\r\n",
      "[i-0615648acb65e87ed] INFO:root:    Time 0.015453 (0.015733)\r\n",
      "[i-0615648acb65e87ed] INFO:root:Epoch 43\r\n",
      "[i-0615648acb65e87ed] INFO:root:--- Accuracy 98.00%\r\n",
      "[i-0615648acb65e87ed] INFO:root:    Time 0.014809 (0.015712)\r\n",
      "[i-0615648acb65e87ed] INFO:root:Epoch 44\r\n",
      "[i-0615648acb65e87ed] INFO:root:--- Accuracy 97.00%\r\n",
      "[i-0615648acb65e87ed] INFO:root:    Time 0.015203 (0.015700)\r\n",
      "[i-0615648acb65e87ed] INFO:root:Epoch 45\r\n",
      "[i-0615648acb65e87ed] INFO:root:--- Accuracy 100.00%\r\n",
      "[i-0615648acb65e87ed] INFO:root:    Time 0.015045 (0.015685)\r\n",
      "[i-0615648acb65e87ed] INFO:root:Epoch 46\r\n",
      "[i-0615648acb65e87ed] INFO:root:--- Accuracy 100.00%\r\n",
      "[i-0615648acb65e87ed] INFO:root:    Time 0.015024 (0.015671)\r\n",
      "[i-0615648acb65e87ed] INFO:root:Epoch 47\r\n",
      "[i-0615648acb65e87ed] INFO:root:--- Accuracy 100.00%\r\n",
      "[i-0615648acb65e87ed] INFO:root:    Time 0.015204 (0.015661)\r\n",
      "[i-0615648acb65e87ed] INFO:root:Epoch 48\r\n",
      "[i-0615648acb65e87ed] INFO:root:--- Accuracy 100.00%\r\n",
      "[i-0615648acb65e87ed] INFO:root:    Time 0.015640 (0.015661)\r\n",
      "[i-0615648acb65e87ed] INFO:root:Epoch 49\r\n",
      "[i-0615648acb65e87ed] INFO:root:--- Accuracy 100.00%\r\n",
      "[i-0615648acb65e87ed] INFO:root:    Time 0.014968 (0.015647)\r\n",
      "[i-0615648acb65e87ed] INFO:root:Epoch 50\r\n",
      "[i-0615648acb65e87ed] INFO:root:--- Accuracy 100.00%\r\n",
      "[i-0615648acb65e87ed] INFO:root:    Time 0.015066 (0.015635)\r\n",
      "[i-0615648acb65e87ed] INFO:root:CrypTen Weights:\r\n",
      "[i-0615648acb65e87ed] INFO:root:tensor([[ 0.6266,  0.0708, -0.8273,  1.1054,  2.2259,  1.0269, -0.2454,  0.3465,\r\n",
      "[i-0615648acb65e87ed]          -0.2064,  1.1096, -0.4470, -0.9675,  0.8815, -0.0456, -0.3211, -0.4172,\r\n",
      "[i-0615648acb65e87ed]          -0.1068, -0.4309,  1.6191,  0.9273,  1.3629, -0.4860, -0.4709,  0.1397,\r\n",
      "[i-0615648acb65e87ed]          -0.1461, -1.2914,  0.5050, -0.2048,  0.1133, -0.6340,  0.1254, -0.7410,\r\n",
      "[i-0615648acb65e87ed]           0.1307,  1.9361, -1.3594, -0.3963, -1.2906,  1.4484, -0.7685,  0.2926,\r\n",
      "[i-0615648acb65e87ed]           0.8602,  0.3248,  0.0922, -0.4171,  0.7996,  0.2740,  0.5352,  0.6995,\r\n",
      "[i-0615648acb65e87ed]           0.6882,  0.6521]])\r\n",
      "[i-0615648acb65e87ed] INFO:root:CrypTen Bias:\r\n",
      "[i-0615648acb65e87ed] INFO:root:tensor([0.2052])\r\n",
      "[i-0615648acb65e87ed] \n"
     ]
    }
   ],
   "source": [
    "%%script bash\n",
    "python3 [PATH_TO_CRYPTEN]/CrypTen/scripts/aws_launcher.py \\\n",
    "--ssh_key_file [SSH_KEY_FILE] --instances=[AWS_INSTANCE1, AWS_INSTANCE2...] \\\n",
    "--region [AWS_REGION] \\\n",
    "--ssh_user [AWS_USERNAME] \\\n",
    "--aux_files=[PATH_TO_CRYPTEN]/CrypTen/examples/mpc_linear_svm/mpc_linear_svm.py [PATH_TO_CRYPTEN]/CrypTen/examples/mpc_linear_svm/launcher.py \\\n",
    "--features 50 \\\n",
    "--examples 100 \\\n",
    "--epochs 50 \\\n",
    "--lr 0.5 \\\n",
    "--skip_plaintext"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
